package com.fourth.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjy
 * @create 2022-08-20 9:19
 */
public class KeywordUtil {
    //定义一个返回分词结果的集合的方法
    public static List<String> splitKeyword(String keyword) throws IOException {

        //创建list集合用于用于存放分词后的数据
        ArrayList<String> resultList = new ArrayList<>();

        //创建IK分词器对象
        StringReader reader = new StringReader(keyword);
        IKSegmenter ikSegmenter = new IKSegmenter(reader,true);

        //切词&将切好的词放入集合
        Lexeme next = ikSegmenter.next();
        while (next != null){
            String word = next.getLexemeText();
            resultList.add(word);

            next = ikSegmenter.next();
        }
        //工具类遇到异常一般都往上抛，很多程度都可能使用这个工具类，所以异常分开单独处理更好
        return resultList;
    }
}
